<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goods_Model extends CI_Model
{
    public function getList($limit, $offset=0, $select = '', $value = '')
    {
        if(0 < $limit)
        {
            if($select == '' && $value == '')
            {
                $sql = "SELECT TOP $limit * FROM G_Goods_GoodsList WHERE (GoodsNO NOT IN (SELECT TOP $offset GoodsNO FROM G_Goods_GoodsList ORDER BY GoodsNO)) ORDER BY GoodsNO";
            }
            else
            {
                $value = iconv('UTF-8', 'GB2312', $value);
                $sql = "SELECT TOP $limit * FROM G_Goods_GoodsList WHERE (GoodsNO NOT IN (SELECT TOP $offset GoodsNO FROM G_Goods_GoodsList where $select like '%$value%' ORDER BY GoodsNO)) and $select like '%$value%' ORDER BY GoodsNO";
            }
            $query = $this->db->query($sql);
            return $this->_setup($query->result());
        }
        else
            return false;
    }
    
    private function _setup($items)
    {
        if( ! empty($items))
        {
            foreach($items as $k => $m)
            //$items[$k]->url = site_url('goods/show/id/'.$m->id);
            {
                //查询商品对应供应商，供应商货号,供应商价格
                $provider = $this->getProviderByGoodsId($items[$k]->GoodsID);
                if($provider)
                {
                    $provider[0]->ProviderName = iconv('GB2312', 'UTF-8', $provider[0]->ProviderName);
                    $items[$k]->ProviderName = $provider[0]->ProviderName;
                    $items[$k]->ProviderGoodsNO = $provider[0]->GoodsNO;
                }
                else
                {
                    $items[$k]->ProviderName = '';
                    $items[$k]->ProviderGoodsNO = '';
                }
                
                //查询商品标记状态
                if($items[$k]->FlagID != '')
                {
                    $flag = $this->getFlagByFlagId($items[$k]->FlagID);
                    $flag[0]->FlagName = iconv('GB2312', 'UTF-8', $flag[0]->FlagName);
                    $items[$k]->FlagName = $flag[0]->FlagName;
                }
                else
                {
                    $items[$k]->FlagName = '';
                }
                
                //查询商品实际库存,警戒库存
                $stock = $this->getStockByGoodsId($items[$k]->GoodsID);
                if($stock)
                {
                    //echo $stock[$k]->stockWarTotal;exit;
                    $items[$k]->RealStock = $stock['stockTotal'];
                    $items[$k]->RealWarStock = $stock['stockWarTotal'];
                }
                else
                {
                    $items[$k]->RealStock = '0';
                    $items[$k]->RealWarStock = '0';
                }
                
                //转换商品名称编码
                $items[$k]->GoodsNO = iconv('GB2312', 'UTF-8', $items[$k]->GoodsNO);
                $items[$k]->GoodsName = iconv('GB2312', 'UTF-8', $items[$k]->GoodsName);
                
                //组装多规格商品查看连接
                if($items[$k]->bMultiSpec == 0)
                {
                    $items[$k]->StockUrl = site_url('goods/specstock/'.$items[$k]->GoodsID);
                }
                else
                {
                    $items[$k]->StockUrl = site_url('goods/specstock/'.$items[$k]->GoodsID);
                }
                
            }
            return $items;	
        }	
        else
            return false;
    }
    
    public function getTotalCount($select = '', $value = '')
    {
        if($select == '' && $value == '')
        {
            return $this->db->count_all('G_Goods_GoodsList');
        }
        else
        {
            $value = iconv('UTF-8', 'GB2312', $value);
            $this->db->like(array($select=>$value));
            return $this->db->count_all_results('G_Goods_GoodsList');
        }
    }
    
    public function getProviderByGoodsId($goodsid)
    {
        $query = $this->db->get_where('G_Goods_Provider', array('GoodsID'=>$goodsid, 'bDefault'=>True));
        $result = $query->result();
        if( !empty ($result))
        {
            foreach($result as $k => $m)
            {
                $query = $this->db->get_where('G_Cfg_ProviderList', array('ProviderID'=>$result[$k]->ProviderID));
                $query1 = $query->result();
                foreach($query1 as $k => $m)
                {
                    $result[$k]->ProviderName = $query1[$k]->ProviderName;
                } 
            }
            return $result;
        }
        else
            return FALSE;
    }
    
    public function getFlagByFlagId($flag)
    {
        $query = $this->db->get_where('G_Cfg_RecordFlag', array('FlagID'=>$flag));
        $result = $query->result();
        if( !empty ($result))
        {
            return $result;
        }
        else
            return FALSE;
    }
    
    public function getStockByGoodsId($goodsid)
    {
        $query = $this->db->get_where('G_Stock_Spec', array('GoodsID'=>$goodsid, 'WarehouseID'=>'1000'));
        $result = $query->result();
        $array = array('stockTotal'=>0, 'stockWarTotal'=>0);
        
        if( !empty ($result))
        {
            foreach ($result as $k => $m)
            {
                if($result[$k]->RealStock == '')
                {
                    $result[$k]->RealStock = 0;
                }
                if($result[$k]->RealWarStock == '')
                {
                    $result[$k]->RealWarStock = 0;
                }
                
                $array['stockTotal'] += $result[$k]->RealStock;
                $array['stockWarTotal'] += $result[$k]->RealWarStock;
            }
            return $array;
        }
        else
            return FALSE;
    }
    
    public function getSpecStockByGoodsId($goodsid)
    {
        $query = $this->db->get_where('G_Stock_Spec', array('GoodsID'=>$goodsid, 'WarehouseID'=>'1000'));
        $result = $query->result();
        
        if( !empty ($result))
        {
            foreach ($result as $k => $m)
            {
                if($result[$k]->RealStock == '')
                {
                    $result[$k]->RealStock = 0;
                }
                if($result[$k]->RealWarStock == '')
                {
                    $result[$k]->RealWarStock = 0;
                }
                
                $query = $this->db->get_where('G_Goods_GoodsSpec', array('SpecID'=>$result[$k]->SpecID));
                $resultSpec = $query->result();
                if($resultSpec[0]->SpecID == 0)
                    $result[$k]->SpecName = '单规格';
                else
                    $result[$k]->SpecName = iconv('GB2312', 'UTF-8', $resultSpec[0]->SpecName);
                
                $this->db->select('GoodsNO');
                $query = $this->db->get_where('G_Goods_GoodsList', array('GoodsID'=>$goodsid));
                $resultGoodsNO = $query->result();
                $result[$k]->GoodsNO = iconv('GB2312', 'UTF-8', $resultGoodsNO[0]->GoodsNO);
            }
            return $result;
        }
        else
            return FALSE;
    }
    
    public function modifySpecStockByGoodsId($goodsid)
    {
        $array = $_POST;
        if(array_pop($array))
        {
            foreach ($array as $key => $value) 
            {
                if($array[$key]['stock'] == '')
                {
                    $array[$key]['stock'] == 0;
                }
                if($array[$key]['opselect'] == 'add')
                {
                    $data = array(
                       'RealStock' => $array[$key]['relstock']+$array[$key]['stock'],
                       'RealWarStock' => (int)$array[$key]['stockWar']
                    );
                }
                else
                {
                    $data = array(
                       'RealStock' => $array[$key]['relstock']-$array[$key]['stock'],
                       'RealWarStock' => (int)$array[$key]['stockWar']
                    );
                }

                if($key == 0)
                {
                    $this->db->where('GoodsID', $goodsid);
                    $this->db->update('G_Stock_Spec', $data); 
                }
                else
                {
                    $this->db->where('SpecID', $key);
                    $this->db->update('G_Stock_Spec', $data);
                }
            }
        }
        else
        {
            return FALSE;
        }
        return TRUE;
    }
}
